﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;

namespace RiskMan.DataClass.Models.Plaza_II.FortsInfoRepl
{
    /// <summary>
    /// Таблица 67. Поля таблицы base_contracts_params - Параметры базовых контрактов
    /// </summary>
    public class BaseContractsParams: INotifyPropertyChanged
    {
        #region private

        private long _replId;
        private long _replRev;
        private long _replAct;
        private string _codeVcb;
        private string _codeMcs;
        private int _volatNum;
        private int _pointsNum;
        private Decimal _subrickStep;
        private int _isPercent;
        private Decimal _percentRate;
        private Decimal _curencyVolat;
        private int _isUsd;
        private Decimal _usdRateCurvRadius;
        private Decimal _somc;

        #endregion


        #region public

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplId
        {
            get { return _replId; }
            set
            {
                if (_replId != value)
                {
                    _replId = value;
                    RaisePropertyChanged("ReplId");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplRev
        {
            get { return _replRev; }
            set
            {
                if (_replRev != value)
                {
                    _replRev = value;
                    RaisePropertyChanged("ReplRev");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplAct
        {
            get { return _replAct; }
            set
            {
                if (_replAct != value)
                {
                    _replAct = value;
                    RaisePropertyChanged("ReplAct");
                }
            }
        }
        
        /// <summary>
        /// Код базового контракта
        /// </summary>
        public string CodeVcb
        {
            get { return _codeVcb; }
            set
            {
                if (_codeVcb != value)
                {
                    _codeVcb = value;
                    RaisePropertyChanged("CodeVcb");
                }
            }
        }
        
        /// <summary>
        /// Код межконтрактного спрэда
        /// </summary>
        public string CodeMcs
        {
            get { return _codeMcs; }
            set
            {
                if (_codeMcs != value)
                {
                    _codeMcs = value;
                    RaisePropertyChanged("CodeMcs");
                }
            }
        }
        
        /// <summary>
        /// Количество кривых волатильности
        /// </summary>
        public int VolatNum
        {
            get { return _volatNum; }
            set
            {
                if (_volatNum != value)
                {
                    _volatNum = value;
                    RaisePropertyChanged("VolatNum");
                }
            }
        }
        
        /// <summary>
        /// Количество точек риска
        /// </summary>
        public int PointsNum
        {
            get { return _pointsNum; }
            set
            {
                if (_pointsNum != value)
                {
                    _pointsNum = value;
                    RaisePropertyChanged("PointsNum");
                }
            }
        }
        
        /// <summary>
        /// Шаг подточек риска
        /// </summary>
        public Decimal SubrickStep
        {
            get { return _subrickStep; }
            set
            {
                if (_subrickStep != value)
                {
                    _subrickStep = value;
                    RaisePropertyChanged("SubrickStep");
                }
            }
        }
        
        /// <summary>
        /// Признак процентного контракта
        /// </summary>
        public int IsPercent
        {
            get { return _isPercent; }
            set
            {
                if (_isPercent != value)
                {
                    _isPercent = value;
                    RaisePropertyChanged("IsPercent");
                }
            }
        }
        
        /// <summary>
        /// Процентная ставка (для контрактов на ставки)
        /// </summary>
        public Decimal PercentRate
        {
            get { return _percentRate; }
            set
            {
                if (_percentRate != value)
                {
                    _percentRate = value;
                    RaisePropertyChanged("PercentRate");
                }
            }
        }
        
        /// <summary>
        /// Волатильность курса валюты
        /// </summary>
        public Decimal CurencyVolat
        {
            get { return _curencyVolat; }
            set
            {
                if (_curencyVolat != value)
                {
                    _curencyVolat = value;
                    RaisePropertyChanged("CurencyVolat");
                }
            }
        }
        
        /// <summary>
        /// Признак расчета в долларах
        /// </summary>
        public int IsUsd
        {
            get { return _isUsd; }
            set
            {
                if (_isUsd != value)
                {
                    _isUsd = value;
                    RaisePropertyChanged("IsUsd");
                }
            }
        }
        
        /// <summary>
        /// Радиус кривизны курса валюты
        /// </summary>
        public Decimal UsdRateCurvRadius
        {
            get { return _usdRateCurvRadius; }
            set
            {
                if (_usdRateCurvRadius != value)
                {
                    _usdRateCurvRadius = value;
                    RaisePropertyChanged("UsdRateCurvRadius");
                }
            }
        }
        
        /// <summary>
        /// Ставка ГО по непокрытым продажам (в рублях)
        /// </summary>
        public Decimal Somc
        {
            get { return _somc; }
            set
            {
                if (_somc != value)
                {
                    _somc = value;
                    RaisePropertyChanged("Somc");
                }
            }
        }


        #endregion


        #region event PropertyChangedEventHandler

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Processing
        public static BaseContractsParams ParseBaseContractsParams(Dictionary<String, String> relation)
        {
            BaseContractsParams tmp = new BaseContractsParams();
            tmp.Parse(relation);
            return tmp;
        }

        public void Parse(Dictionary<String, String> relation)
        {
            foreach (String field in relation.Keys)
            {
                String value = relation[field];

                switch (field)
                {
                    case "replID": long.TryParse(value, out _replId); break;
                    case "replRev": long.TryParse(value, out _replRev); break;
                    case "replAct": long.TryParse(value, out _replAct); break;
                    case "code_vcb": _codeVcb = value; break;
                    case "code_mcs": _codeMcs = value; break;
                    case "volat_num": int.TryParse(value, out _volatNum); break;
                    case "points_num": int.TryParse(value, out _pointsNum); break;
                    case "subrick_step": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _subrickStep); break;
                    case "is_percent": int.TryParse(value, out _isPercent); break;
                    case "percent_rate": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _percentRate); break;
                    case "curency_volat": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _curencyVolat); break;
                    case "is_usd": int.TryParse(value, out _isUsd); break;
                    case "usd_rate_curv_radius": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _usdRateCurvRadius); break;
                    case "somc": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _somc); break; 

                };
            }
        }

        public static void AddValueToObsColBaseContractsParams(BaseContractsParams inst)
        {
            MainWindow.StaticLevel._dataProvider.BaseContractsParamsList.Add(inst);
        }
        #endregion
    }
}
